package com.bk.controller.user;
import com.bk.constant.JwtClaimsConstant;
import com.bk.dto.UserDTO;
import com.bk.dto.UserLoginDTO;
import com.bk.dto.UserRegisterDTO;
import com.bk.entity.User;
import com.bk.properties.JwtProperties;
import com.bk.result.Result;
import com.bk.service.UserService;
import com.bk.utils.JwtUtil;
import com.bk.vo.UserLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
/**
 * 用户管理
 */
@RestController
@RequestMapping("/user/user")
@Slf4j
@Api(tags = "用户相关接口")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     * @param userLoginDTO
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "用户登录")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("用户登录：{}", userLoginDTO);

        User user = userService.login(userLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        UserLoginVO userLoginVO = UserLoginVO.builder()
                .id(user.getId())
                .userName(user.getUsername())
                .token(token)
                .build();

        return Result.success(userLoginVO);
    }
    /**
     * 注册
     * @param userRegisterDTO
     * @return
     */
    @PostMapping
    @ApiOperation(value = "用户注册")
    public Result userRegister(@RequestBody UserRegisterDTO userRegisterDTO)
    {

        return Result.success();
    }
    /**
     * 退出
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("用户退出")
    public Result<String> logout() {
        return Result.success();
    }


    @GetMapping("/{id}")
    @ApiOperation("根据id来查询用户信息")
    public Result<User> getById(@PathVariable Long id)
    {
        User user = userService.getById(id);
        return Result.success(user);
    }

    /**
     * 修改个人信息
     */
    @PutMapping("/message")
    @ApiOperation("修改个人信息")
    public Result updateMessage(@RequestBody UserDTO userDTO)
    {
       return Result.success();
    }

    /**
     * 修改密码
     * @param newPassword  新密码
     * @return
     */
    @PutMapping("/password")
    @ApiOperation("修改密码")
    public Result updatePassword(String username,String newPassword)
    {
        return Result.success();
    }



}
